From e3d77473495ba5166de10133b21b6ef3b3404aa3 Mon Sep 17 00:00:00 2001 From: "emellor@leeni.uk.xensource.com" Date: Thu, 1 Dec 2005 03:39:23 +0000 Subject: [PATCH] Move the dom0 ballooning-out code from the xm create handler and into the server, where it can be used on reboot and migrate as well as create. Ballooning bugs may still exist (it is unclear) but this is certainly a good step in the right direction. Ignore the memory and maxmem entries in the store for dom0 when restarting. This means that the value in use at the time becomes the configured target. This should fix the bug whereby dom0_mem settings on the command line are being overridden by (older) entries in the store. Signed-off-by: Ewan Mellor --- tools/python/xen/xend/XendCheckpoint.py | 5 +- tools/python/xen/xend/XendDomainInfo.py | 27 +++++-- tools/python/xen/xend/balloon.py | 97 +++++++++++++++++++++++++ tools/python/xen/xm/create.py | 60 --------------- 4 files changed, 121 insertions(+), 68 deletions(-) create mode 100644 tools/python/xen/xend/balloon.py diff --git a/tools/python/xen/xend/XendCheckpoint.py b/tools/python/xen/xend/XendCheckpoint.py index 06c51ee417..8d80bbc45b 100644 --- a/tools/python/xen/xend/XendCheckpoint.py +++ b/tools/python/xen/xend/XendCheckpoint.py @@ -18,6 +18,7 @@ import xen.util.auxbin import xen.lowlevel.xc +import balloon from XendError import XendError from XendLogging import log @@ -131,6 +132,8 @@ def restore(xd, fd): raise XendError( "not a valid guest state file: pfn count out of range") + balloon.free(xc.pages_to_kib(nr_pfns)) + cmd = map(str, [xen.util.auxbin.pathTo(XC_RESTORE), xc.handle(), fd, dominfo.getDomid(), nr_pfns, store_port, console_port]) @@ -213,4 +216,4 @@ def slurp(infile): if line == "": break else: - log.error('%s', line) + log.error('%s', line.strip()) diff --git a/tools/python/xen/xend/XendDomainInfo.py b/tools/python/xen/xend/XendDomainInfo.py index 8f01f0aef0..748db3b3a4 100644 --- a/tools/python/xen/xend/XendDomainInfo.py +++ b/tools/python/xen/xend/XendDomainInfo.py @@ -33,6 +33,7 @@ import xen.lowlevel.xc from xen.util import asserts from xen.util.blkif import blkdev_uname_to_file +import balloon import image import sxp import uuid @@ -226,13 +227,13 @@ def recreate(xeninfo, priv): 'Uuid in store does not match uuid for existing domain %d: ' '%s != %s' % (domid, uuid2_str, xeninfo['uuid'])) - vm = XendDomainInfo(xeninfo, domid, dompath, True) + vm = XendDomainInfo(xeninfo, domid, dompath, True, priv) except Exception, exn: if priv: log.warn(str(exn)) - vm = XendDomainInfo(xeninfo, domid, dompath, True) + vm = XendDomainInfo(xeninfo, domid, dompath, True, priv) vm.removeDom() vm.removeVm() vm.storeVmDetails() @@ -369,7 +370,8 @@ def dom_get(dom): class XendDomainInfo: - def __init__(self, info, domid = None, dompath = None, augment = False): + def __init__(self, info, domid = None, dompath = None, augment = False, + priv = False): self.info = info @@ -387,7 +389,7 @@ class XendDomainInfo: self.dompath = dompath if augment: - self.augmentInfo() + self.augmentInfo(priv) self.validateInfo() @@ -443,7 +445,7 @@ class XendDomainInfo: return 1 - def augmentInfo(self): + def augmentInfo(self, priv): """Augment self.info, as given to us through {@link #recreate}, with values taken from the store. This recovers those values known to xend but not to the hypervisor. @@ -452,8 +454,15 @@ class XendDomainInfo: if not self.infoIsSet(name) and val is not None: self.info[name] = val - map(lambda x, y: useIfNeeded(x[0], y), VM_STORE_ENTRIES, - self.readVMDetails(VM_STORE_ENTRIES)) + if priv: + entries = VM_STORE_ENTRIES[:] + entries.remove(('memory', int)) + entries.remove(('maxmem', int)) + else: + entries = VM_STORE_ENTRIES + + map(lambda x, y: useIfNeeded(x[0], y), entries, + self.readVMDetails(entries)) device = [] for c in controllerClasses: @@ -842,6 +851,9 @@ class XendDomainInfo: """Set the memory target of this domain. @param target In MiB. """ + log.debug("Setting memory target of domain %s (%d) to %d MiB.", + self.info['name'], self.domid, target) + self.info['memory'] = target self.storeVm("memory", target) self.storeDom("memory/target", target << 10) @@ -1105,6 +1117,7 @@ class XendDomainInfo: xc.domain_setcpuweight(self.domid, self.info['cpu_weight']) m = self.image.getDomainMemory(self.info['memory'] * 1024) + balloon.free(m) xc.domain_setmaxmem(self.domid, m) xc.domain_memory_increase_reservation(self.domid, m, 0, 0) diff --git a/tools/python/xen/xend/balloon.py b/tools/python/xen/xend/balloon.py new file mode 100644 index 0000000000..69cb465c1c --- /dev/null +++ b/tools/python/xen/xend/balloon.py @@ -0,0 +1,97 @@ +#=========================================================================== +# This library is free software; you can redistribute it and/or +# modify it under the terms of version 2.1 of the GNU Lesser General Public +# License as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +#============================================================================ +# Copyright (C) 2004, 2005 Mike Wray +# Copyright (C) 2005 XenSource Ltd +#============================================================================ + + +import time + +import xen.lowlevel.xc + +import XendDomain +import XendRoot +from XendLogging import log +from XendError import VmError + + +PROC_XEN_BALLOON = "/proc/xen/balloon" +BALLOON_OUT_SLACK = 1 # MiB. We need this because the physinfo details are + # rounded. + + +def free(required): + """Balloon out memory from the privileged domain so that there is the + specified required amount (in KiB) free. + """ + + xc = xen.lowlevel.xc.xc() + xroot = XendRoot.instance() + + try: + free_mem = xc.physinfo()['free_memory'] + need_mem = (required + 1023) / 1024 + BALLOON_OUT_SLACK + + log.debug("Balloon: free %d; need %d.", free_mem, need_mem) + + if free_mem >= need_mem: + return + + dom0_min_mem = xroot.get_dom0_min_mem() + if dom0_min_mem == 0: + raise VmError('Not enough free memory and dom0_min_mem is 0.') + + dom0_alloc = _get_dom0_alloc() + dom0_new_alloc = dom0_alloc - (need_mem - free_mem) + if dom0_new_alloc < dom0_min_mem: + raise VmError( + ('I need %d MiB, but dom0_min_mem is %d and shrinking to ' + '%d MiB would leave only %d MiB free.') % + (need_mem, dom0_min_mem, dom0_min_mem, + free_mem + (dom0_alloc - dom0_min_mem))) + + dom0 = XendDomain.instance().privilegedDomain() + dom0.setMemoryTarget(dom0_new_alloc) + + timeout = 20 # 2 sec + while timeout > 0: + time.sleep(0.1) + + free_mem = xc.physinfo()['free_memory'] + if free_mem >= need_mem: + return + + timeout -= 1 + + raise VmError('The privileged domain did not balloon!') + finally: + del xc + + +def _get_dom0_alloc(): + """Return current allocation memory of dom0 (in MiB). Return 0 on error""" + + f = file(PROC_XEN_BALLOON, 'r') + try: + line = f.readline() + for x in line.split(): + for n in x: + if not n.isdigit(): + break + else: + return int(x) / 1024 + return 0 + finally: + f.close() diff --git a/tools/python/xen/xm/create.py b/tools/python/xen/xm/create.py index d3756d3adc..211474dc7e 100644 --- a/tools/python/xen/xm/create.py +++ b/tools/python/xen/xm/create.py @@ -798,57 +798,6 @@ def make_domain(opts, config): opts.info("Started domain %s" % (dom)) return int(sxp.child_value(dominfo, 'domid')) -def get_dom0_alloc(): - """Return current allocation memory of dom0 (in MB). Return 0 on error""" - PROC_XEN_BALLOON = "/proc/xen/balloon" - - f = open(PROC_XEN_BALLOON, "r") - line = f.readline() - for x in line.split(): - for n in x: - if not n.isdigit(): - break - else: - f.close() - return int(x)/1024 - f.close() - return 0 - -def balloon_out(dom0_min_mem, opts): - """Balloon out memory from dom0 if necessary""" - SLACK = 4 - timeout = 20 # 2s - ret = 1 - - xc = xen.lowlevel.xc.xc() - free_mem = xc.physinfo()['free_pages'] / 256 - domU_need_mem = opts.vals.memory + SLACK - - # we already have enough free memory, return success - if free_mem >= domU_need_mem: - del xc - return 0 - - dom0_cur_alloc = get_dom0_alloc() - dom0_new_alloc = dom0_cur_alloc - (domU_need_mem - free_mem) - if dom0_new_alloc < dom0_min_mem: - dom0_new_alloc = dom0_min_mem - - server.xend_domain_mem_target_set(0, dom0_new_alloc) - - while timeout > 0: - time.sleep(0.1) # sleep 100ms - - free_mem = xc.physinfo()['free_pages'] / 256 - if free_mem >= domU_need_mem: - ret = 0 - break - timeout -= 1 - - del xc - return ret - - def parseCommandLine(argv): gopts.reset() args = gopts.parse(argv) @@ -891,15 +840,6 @@ def main(argv): if opts.vals.dryrun: PrettyPrint.prettyprint(config) else: - from xen.xend import XendRoot - - xroot = XendRoot.instance() - - dom0_min_mem = xroot.get_dom0_min_mem() - if dom0_min_mem != 0: - if balloon_out(dom0_min_mem, opts): - err("cannot allocate enough memory for domain") - dom = make_domain(opts, config) if opts.vals.console_autoconnect: console.execConsole(dom) -- 2.30.2